package com.comparable_comparator.arraylist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Car {
	private int carId;
	private String carName;
	private String modelYear;

	public Car() {
	}

	public Car(int carId, String carName, String modelYear) {
		this.carId = carId;
		this.carName = carName;
		this.modelYear = modelYear;
	}

	public void setCarId(int carId) {
		this.carId = carId;
	}

	public int getCarId() {
		return carId;
	}

	public void setCarName(String carName) {
		this.carName = carName;
	}

	public String getCarName() {
		return carName;
	}

	public void setModelYear(String modelYear) {
		this.modelYear = modelYear;
	}

	public String getModelYear() {
		return modelYear;
	}

	public String toString() {
		return carId + "\t" + carName + "\t" + modelYear;
	}
}

class SortByCarName implements Comparator {

	public int compare(Object o1, Object o2) {
	Car c1 = (Car) o1;
	Car c2 = (Car) o2;
	String name1=c1.getCarName();
	String name2=c2.getCarName();
	//return -name1.compareTo(name2);
	int n1=(int)name1.length();
	int n2=(int)name2.length();
	if(n1>n1)return +1;
	else if(n1<n2)return -1;
	else return 0;

	}

}

public class CarType {
	public static void main(String[] args) {
		List list = new ArrayList();
		list.add(new Car(3, "a", "2012"));
		list.add(new Car(1, "z", "2015"));
		list.add(new Car(2, "b", "2010"));
		list.add(new Car(5, "d", "2009"));
		list.add(new Car(4, "e", "2014"));
		for (Object c : list) {
			System.out.println(c);
		}
		System.out.println("-----------Sort By Name of the Car---------");
		Collections.sort(list, new SortByCarName());
		for (Object c2 : list) {
			System.out.println(c2);
		}
	}
}
